package com.dgut.web.Controller;

import com.dgut.jpa.Entity.Umessage;
import com.dgut.jpa.Entity.User;
import com.dgut.jpa.Dao.UmessageDao;
import com.dgut.jpa.Dao.UserDao;
import net.sf.json.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

@RestController
@SpringBootApplication
public class indexController {

    @Resource
    UserDao userDao;

    @Resource
    UmessageDao umessageDao;


    @RequestMapping("/login")
    public Serializable login(String openid) {
        JSONObject json = new JSONObject();
        List<User> list = userDao.findAll();
        for(int g=0;g<list.size();g++){
            User us = list.get(g);
            if(openid.equals(us.getOpenid())){
                json.put("a", us);
                System.out.println(json);

                return json;
            }

        }
        return "1";
    }

    @RequestMapping("/panduan")
    public Map<String, Object> panduan(String openid) {
        JSONObject json = new JSONObject();
        String q="0";
        List<User> list = userDao.findAll();
        for(int g=0;g<list.size();g++){
            User us = list.get(g);
            if(openid.equals(us.getOpenid())){
                if(q.equals(us.getPower())){
                    json.put("a", "1");
                    return json;
                }
            }

        }
        json.put("a", 2);
        return json;
    }

    @RequestMapping("/test")
    @ResponseBody
    public Map<String, Object> getWxUserOpenid(String code) {
        //拼接url
        StringBuilder url = new StringBuilder("https://api.weixin.qq.com/sns/jscode2session?appid=wxc191b538f17f4a00&secret=abab1fa2fd42c0ba4076176d394b27e9");
        url.append("&js_code=");//code设置
        url.append(code);
        url.append("&grant_type=authorization_code");
        Map<String, Object> map = null;
        try {
            HttpClient client = HttpClientBuilder.create().build();//构建一个Client
            HttpGet get = new HttpGet(url.toString());    //构建一个GET请求
            HttpResponse response = client.execute(get);//提交GET请求
            HttpEntity result = response.getEntity();//拿到返回的HttpResponse的"实体"
            String content = EntityUtils.toString(result);
            System.out.println(content);//打印返回的信息
            JSONObject res = JSONObject.fromObject(content);//把信息封装为json
            //把信息封装到map
            map = MdzwUtils.parseJSON2Map(res);
        } catch (Exception e) {
            e.printStackTrace();
        }
        User user = new User();
        int i = 1;
        Iterator<String> iter = map.keySet().iterator();
        while (iter.hasNext()) {
            String key = iter.next();
            String value = (String) map.get(key);
            if (i == 1) {
                user.setOpenid(value);
            }
            i++;
        }
        String a = user.getOpenid();
        System.out.println(a);
        List<User> list = userDao.findAll();
        for(int z=0;z<list.size();z++){
            User us = list.get(z);
            System.out.println("openid"+us.getOpenid());
            if(a.equals(us.getOpenid())){
                map.put("session_key", 2);
                return map;
            }
        }
        map.put("session_key", 1);
        return map;
    }


    @RequestMapping("/register")
    public String addUser(String openid, String username, String password, String name, String idcard, String live, String head) {
        //User user = new User("4578","1111","11111","11111","11111","5555");
        User user = new User();
        user.setOpenid(openid);
        user.setAccount(username);
        user.setPassword(password);
        user.setName(name);
        user.setCardid(idcard);
        user.setHouseid(live);
        user.setPower("0");
        user.setHead(head);
        //Example<User> example = Example.of(user);
        //userDao.findOne(example);
        userDao.save(user);
        return "succeed";
    }


    @RequestMapping("/addmessage")
    public String addMessage(String cardid, String houseid, String name, double temperature, String in_orout, Date outtime) {
        //User user = new User("4578","1111","11111","11111","11111","5555");
        System.out.println(houseid);
       Umessage umessage=new Umessage();
       umessage.setCardid(cardid);
       umessage.setHouseid(houseid);
       umessage.setName(name);
       umessage.setTemperature(temperature);
       umessage.setInOrout(in_orout);
       umessage.setTime(outtime);
       umessageDao.save(umessage);
        return "succeed";
    }
}